<?php

class Commentary
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 10;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'comments','searchCommentary','recent','addComment','editLike','commentOnlineList','addCommentOnline','editLikeOnline','searchCommentOnline'
        );
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    private function recent($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $result = array(
            'comments'      => $res['comment'],
            'anonymousInfo' => $res['info'],
        );
        $req['position'] = 0;
        if ($result['comments']) $req['position'] = $result['comments'][count($result['comments']) -1]['position'];
        $pageUrl = $this->pageUrl($req);
        return render('recent_anonymous', array('list' => $result, 'pageUrl' => $pageUrl));
    }

    private function comments($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $result = array(
            'comments'      => $res['comment'],
            'anonymousInfo' => $res['info'],
        );
        $req['position'] = 0;
        if ($result['comments']) $req['position'] = $result['comments'][count($result['comments']) -1]['position'];
        $pageUrl = $this->pageUrl($req);
        return render('anonymous_comments', array('list' => $result, 'pageUrl' => $pageUrl));
    }

    private function commentOnlineList($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $result = array(
            'comments'      => $res['comment'],
            'anonymousInfo' => $res['info'],
        );
        $req['position'] = 0;
        if ($result['comments']) $req['position'] = $result['comments'][count($result['comments']) -1]['position'];
        $pageUrl = $this->pageUrl($req);
        return render('comment_online', array('list' => $result, 'pageUrl' => $pageUrl));
    }

    //修改点赞值 自动
    private function editLikeOnline($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if ($res){
            echo 1;
        }else{
            echo 0;
        }
        exit();
    }

    //修改点赞值
    private function editLike($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if ($res){
            echo 1;
        }else{
            echo 0;
        }
        exit();
    }

    //添加评论
    private function addComment($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if ($res){
            echo 1;
        }else{
            echo 0;
        }
        exit();
    }

    //添加自动上线的评论
    private function addCommentOnline($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if ($res){
            echo 1;
        }else{
            echo 0;
        }
        exit();
    }

    private function searchCommentary($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $result = array(
            'comments'      => array($res['comment']),
            'anonymousInfo' => $res['info'],
        );
        $req['position'] = 0;
        if ($result['comments']) $req['position'] = $result['comments'][count($result['comments']) -1]['position'];
        $pageUrl = $this->pageUrl($req);
        return render('anonymous_comments', array('list' => $result, 'pageUrl'=>$pageUrl,'bodyId'=>$req['cid']));
    }

    //搜索评论
    private function searchCommentOnline($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $result = array(
            'comments'      => array($res['comment']),
            'anonymousInfo' => $res['info'],
        );
        $req['position'] = 0;
        if ($result['comments']) $req['position'] = $result['comments'][count($result['comments']) -1]['position'];
        $pageUrl = $this->pageUrl($req);
        return render('comment_online', array('list' => $result, 'pageUrl' => $pageUrl));
    }

    private function filterArgumentsOfPassed($req) {
        switch ($this->action) {
            case 'comments':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'comList',
                    'bodyId'  => intval($req['bodyId']),
                    'pos'     => isset($req['position']) ? $req['position'] : 0,
                    'length'  => isset($req['length']) ? $req['length'] : $this->length,
                );
                break;
            case 'commentOnlineList':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'commentOnlineList',
                    'bodyId'  => intval($req['bodyId']),
                    'pos'     => isset($req['position']) ? $req['position'] : 0,
                    'start'   => isset($req['start']) ? $req['start'] : $this->start,
                    'length'  => isset($req['length']) ? $req['length'] : $this->length,
                );
                break;
            case 'recent':
                $param = array(
                    'do'     => 'anonymousManage',
                    'format' => 'json',
                    'operate' => 'recentComment',
                    'pos'     => isset($req['position']) ? $req['position'] : 0,
                );
                break;
            case 'searchCommentary':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'searchComment',
                    'pos'     => isset($req['pos']) ? $req['pos'] : 0,
                    'bodyId'  => isset($req['cid']) ? $req['cid'] : 0,
                );
                break;
            case 'searchCommentOnline':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'searchCommentOnline',
                    'pos'     => isset($req['pos']) ? $req['pos'] : 0,
                    'bodyId'  => isset($req['cid']) ? $req['cid'] : 0,
                );
                break;
            case 'addComment':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'add',
                    'content' => isset($req['content']) ? $req['content'] : 0,
                    'bodyId'  => isset($req['bodyId']) ? $req['bodyId'] : 0,
                    'uid'     => $_SESSION['user']['adminUid'],
                );
                break;
            case 'addCommentOnline':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'addCommentOnline',
                    'content' => isset($req['content']) ? $req['content'] : 0,
                    'bodyId'  => isset($req['bodyId']) ? $req['bodyId'] : 0,
                    'uid'     => $_SESSION['user']['adminUid'],
                );
                break;
            case 'editLike':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'editLike',
                    'pos' => isset($req['pos']) ? $req['pos'] : 0,
                    'bodyId'  => isset($req['bodyId']) ? $req['bodyId'] : 0,
                    'praise'  => isset($req['praise']) ? $req['praise'] : 0,
                    'uid'  => isset($req['uid']) ? $req['uid'] : 0,
                );
                break;
            case 'editLikeOnline':
                $param = array(
                    'do'      => 'anonymousManage',
                    'format'  => 'json',
                    'operate' => 'editLikeOnline',
                    'pos' => isset($req['pos']) ? $req['pos'] : 0,
                    'bodyId'  => isset($req['bodyId']) ? $req['bodyId'] : 0,
                    'praise'  => isset($req['praise']) ? $req['praise'] : 0,
                    'uid'  => isset($req['uid']) ? $req['uid'] : 0,
                );
                break;
        }
        return $param;
    }

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "comments":
                $mpurl .= "&bodyId={$req['bodyId']}&position={$req['position']}";
                $pageurl = multi($req['position'], $this->length, $mpurl);
                break;
            case "recent":
                $mpurl .= "&bodyId={$req['bodyId']}&position={$req['position']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
            case "commentOnlineList":
                $mpurl .= "&bodyId={$req['bodyId']}&position={$req['position']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
        }
        $pageurl = multi($req['start'], $this->length, $mpurl);
        return $pageurl;
    }


}


$req = request();
$commentary   = new Commentary($_SC ['apiurl']);
echo $commentary->execute($req['op']);

